﻿using jutehui.Model;
using MyShop.Models;
using MyShop.PublicMethod;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyShop.Service
{
    public class MainService
    {
        private jutehui.BLL.Product _product = null;
        private CustomMethod _custom = null;
        public MainService()
        {
            _product = new jutehui.BLL.Product();
            _custom = new CustomMethod();

        }

        public ResultModel GetNewInfos(int id)
        {
            ResultModel ret = new ResultModel();
            var proList = _product.GetNewData(id);
            if (proList == null || proList.Count == 0)
            {
                ret.Code = -1;
                ret.Info = "最新数据";
            }
            else
            {
                ret.Code = 0;
                SetMemo(proList);
                ret.Data = _custom.ModelToJson(proList);
            }
            return ret;
        }
        public void SetMemo(List<Product> list)
        {
            for(int index=0;index<list.Count;index++)
            {
                list[index].FromWeb = "";
                list[index].Memo = "";
                
            }
        }
        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Product GetProduct(int id)
        { 
            var pro = _product.GetModel(id);
            pro.FromWeb = "";
            pro.Memo = "";
            return pro;
        }

         /// <summary>
         /// 获取一个实体
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
        public ResultModel GetProductById(int id)
        {
            ResultModel ret = new ResultModel();
            var proList = _product.GetModel(id);
            if (proList == null ) 
            {
                ret.Code = -1;
                ret.Info = "没有数据";
            }
            else
            {
                ret.Code = 0;
                proList.FromWeb = "";
                proList.Memo = "";
                ret.Data = _custom.ModelToJson(proList);
            }
            return ret;
        }
        /// <summary>
        /// 获取列表 初始化数据
        /// </summary>
        /// <returns></returns>
        public ResultModel GetProductInfos(int id)
        {
            ResultModel ret = new ResultModel();
            var proList = _product.GetProductByPage(id, StaticClass.PageSize);
            if (proList == null || proList.Count == 0)
            {
                ret.Code = -1;
                ret.Info = "没有数据";
            }
            else
            {
                ret.Code = 0;
                SetMemo(proList);
                ret.Data = _custom.ModelToJson(proList);
            }
            return ret;
        }
        /// <summary>
        /// 加载更多数据  翻页
        /// </summary>
        /// <param name="lastId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public ResultModel GetDataByPageQuery(int lastId, string name,string cateId)
        {
            ResultModel ret = new ResultModel();

            var proList = _product.GetPageModelListByCache((int)NatType.国内, lastId, StaticClass.PageSize, name, cateId, "");
            if (proList == null || proList.Count == 0)
            {
                ret.Code = -1;
                ret.Info = "没有数据";
            }
            else
            {
                ret.Code = 0;
                SetMemo(proList);
                ret.Data = _custom.ModelToJson(proList);
            }
            return ret;
        }
        /// <summary>
        /// 根据分类查询数据
        /// </summary>
        /// <param name="lastId"></param>
        /// <param name="keyName"></param>
        /// <param name="categoryId"></param>
        /// <param name="sourID"></param>
        /// <returns></returns>
        public ResultModel GetDataByCategory(int lastId,string keyName,string categoryId,string sourID)
        {
            ResultModel ret = new ResultModel();
            var name = keyName ?? "";
            var cate = categoryId ?? "";
           
            var sor = sourID ?? "";
            var proList = _product.GetPageModelListByCache((int)NatType.国内, lastId, StaticClass.PageSize, name, cate, sor);
            if (proList == null || proList.Count == 0)
            {
                ret.Code = -1;
                ret.Info = "没有数据";
            }
            else
            {
                ret.Code = 0;
                SetMemo(proList);
                ret.Data = _custom.ModelToJson(proList);
            }
            return ret;
        }
    }
}